package com.hethongviet.job.helpers;

import com.hethongviet.job.R;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class DialogMessage
{
	private Dialog _dialog = null;
	private TextView _textView = null;
	private Button _button = null;
	private Button _buttonYES = null;
	private Activity _activity = null;
	/* For button YES */
	private OnClickListener _onClickListenerYes = null;
	private OnClickListener _onClickListenerNo = null;

	public DialogMessage(Activity activity)
	{
		_activity = activity;
		_dialog = new Dialog(activity);
		_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		_dialog.setContentView(R.layout.dialog_message);
		_textView = (TextView) _dialog.findViewById(R.id.textDialogMessage);
		_button = (Button) _dialog.findViewById(R.id.buttonDialogMessage);
		_buttonYES = (Button) _dialog.findViewById(R.id.buttonDialogMessageYES);
	}

	public DialogMessage(Activity activity, OnClickListener onClickListenerYES)
	{
		this(activity);
		_onClickListenerYes = onClickListenerYES;
	}

	public DialogMessage(Activity activity, OnClickListener onClickListenerYES,
			OnClickListener onClickListenerNO)
	{
		this(activity);
		_onClickListenerYes = onClickListenerYES;
		_onClickListenerNo = onClickListenerNO;
	}

	public void show(int textViewId, final boolean isFinishActivity)
	{
		show(_activity.getString(textViewId), isFinishActivity);
	}

	public void show(String message, final boolean isFinishActivity)
	{
		_textView.setText(message);		

		if (_onClickListenerYes != null && _onClickListenerNo != null)
		{
			_buttonYES.setVisibility(View.VISIBLE);
			_buttonYES.setText(_activity.getString(R.string.textYes));
			_button.setText(_activity.getString(R.string.textNo));
			
			_buttonYES.setOnClickListener(_onClickListenerYes);
			_button.setOnClickListener(_onClickListenerNo);			
		}
		else
		{
			_button.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v)
				{
					_dialog.hide();
					if (isFinishActivity)
					{
						_dialog.dismiss();
						_activity.finish();
					}
				}
			});
			if (_onClickListenerYes != null)
			{
				_buttonYES.setVisibility(View.VISIBLE);
				_buttonYES.setText(_activity.getString(R.string.textYes));
				_button.setText(_activity.getString(R.string.textNo));
				_buttonYES.setOnClickListener(_onClickListenerYes);
			}
			else
			{
				_button.setText(_activity.getString(R.string.textBack));
				_buttonYES.setVisibility(View.GONE);
			}

		}
		try
		{
			_dialog.show();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void hide()
	{
		if (_dialog != null) _dialog.hide();
	}
}
